﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HHCommon;

namespace HHWeb.SystemManage.Solutions
{
    public partial class Dispose : Base
    {
        HHBLL.Solutions B = new HHBLL.Solutions();
        HHModel.Solutions M = new HHModel.Solutions();
        HHCommon.PagePower pagepower = new PagePower();
        Help help = new Help();
        EncAndDec EncAndDec = new EncAndDec();
        //变量定义
        public string Title = string.Empty;
        public string Source = string.Empty;
        public string Author = string.Empty;
        public string PublishTime = string.Empty;
        public int ClickRate =0;
        public string MarketPostion = string.Empty;
        public string SoftVersion = string.Empty;
        public int Sort =0;
        public int Status =1;
        public string Creater = string.Empty;
        public string CreateTime = string.Empty;
        public string TypeInfo = string.Empty;
        public int TypeID =0;
        public string TypeName = string.Empty;
        public string Command = "Add";//执行命令
        protected void Page_Load(object sender, EventArgs e)
        {
            string message = string.Empty;
            try
            {
                if (help.GetHttpMethodBool())
                {
                    Command = Request.Form["Command"].ToString();
                    switch (Command)
                    {
                        case "Add":
                            message = AddSolutions();
                            break;
                        case "Update":
                            message = UpdateSolutions();
                            break;
                        default:
                            break;
                    }
                    if (message.EndsWith("成功！"))
                        MessageBox.ShowAndRedirect(this.Page, message, "List.aspx");
                    else
                        MessageBox.ResponseScript(this.Page,message,"javascript:history.go(-1)");
                }
                else
                {
                    if (help.IFPageParameter())
                    {
                        int ID = Convert.ToInt32(Request.QueryString["ID"].ToString());
                        M = B.GetModel(ID);
                        if (M != null)
                        {
                            Title = M.Title;
                            TypeID = Convert.ToInt32(M.TypeID);
                            TypeName = M.TypeName;
                            FCKeditor1.Value = M.Contents;
                            Source = M.Source;
                            Author = M.Author;
                            MarketPostion = M.MarketPostion;
                            SoftVersion = M.SoftVersion;
                            PublishTime = Convert.ToDateTime(M.publishTime).ToString("yyyy-MM-dd HH:mm:ss");
                            ClickRate = Convert.ToInt32(M.ClickRate);
                            Sort = Convert.ToInt32(M.sort);
                            Status = Convert.ToInt32(M.Status);
                            Creater = M.Creater;
                            CreateTime = Convert.ToDateTime(M.CreateTime).ToString("yyyy-MM-dd HH:mm:ss");
                            Command = "Update";
                        }
                        else
                        {
                            MessageBox.ShowAndRedirect(this.Page, "该数据不存在！", "List.aspx");
                            return;
                        }
                    }
                    else
                    {
                        PublishTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        Creater = EncAndDec.DESDecrypting(help.GetCookie("UserCode"));
                        CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                }
                //产品类别（无限极下拉框）
                GetTypeInfo();
            }
            catch
            {
                MessageBox.ResponseScript(this.Page, "操作错误，请联系管理员！", "javascript:history.go(-1)");
                return;
            }
        }

        #region //产品类别（无限极下拉框）
        public void GetTypeInfo()
        {                
            TypeInfo = pagepower.GetPagePowerInfo("Select [ID] as ID ,[Name] as Name,[ParentID] as ParentID from SolutionTypes with(nolock) where [Status]=1 order by sort desc");
        }
        #endregion

        #region //添加解决方案
        public string AddSolutions()
        {
            M.Title = Request.Form["Title"].ToString();
            M.TypeID = Convert.ToInt32(Request.Form["TypeID"]);
            string TypeName = Request.Form["TypeName"].ToString();
            M.TypeName = TypeName.Remove(0, TypeName.LastIndexOf('-') + 1);
            M.Source = Request.Form["Source"].ToString();
            M.Author = Request.Form["Author"].ToString();
            M.publishTime = Convert.ToDateTime(Request.Form["publishTime"]);
            M.MarketPostion = Request.Form["MarketPostion"].ToString();
            M.SoftVersion = Request.Form["SoftVersion"].ToString();
            M.ClickRate = Convert.ToInt32(Request.Form["ClickRate"]);
            M.Contents = this.FCKeditor1.Value;
            M.sort = Convert.ToInt32(Request.Form["Sort"]);
            M.Status = Convert.ToInt32(Request.Form["Status"]);
            M.Creater = Request.Form["Creater"];
            M.CreateTime = Convert.ToDateTime(Request.Form["CreateTime"]);
            if (B.Add(M) > 0)
                return "添加成功！";
            else
                return "添加失败！";
        }
        #endregion

        #region //修改解决方案
        public string UpdateSolutions()
        {
            M.ID = Convert.ToInt32(Request.QueryString["ID"].ToString());
            M.Title = Request.Form["Title"].ToString();
            M.TypeID = Convert.ToInt32(Request.Form["TypeID"]);
            string TypeName = Request.Form["TypeName"].ToString();
            M.TypeName = TypeName.Remove(0, TypeName.LastIndexOf('-') + 1);
            M.Source = Request.Form["Source"].ToString();
            M.Author = Request.Form["Author"].ToString();
            M.publishTime = Convert.ToDateTime(Request.Form["publishTime"]);
            M.MarketPostion = Request.Form["MarketPostion"].ToString();
            M.SoftVersion = Request.Form["SoftVersion"].ToString();
            M.ClickRate = Convert.ToInt32(Request.Form["ClickRate"]);
            M.Contents = this.FCKeditor1.Value;
            M.sort = Convert.ToInt32(Request.Form["Sort"]);
            M.Status = Convert.ToInt32(Request.Form["Status"]);
            M.Creater = Request.Form["Creater"];
            M.CreateTime = Convert.ToDateTime(Request.Form["CreateTime"]);
            if (B.Update(M))
                return "修改成功！";
            else
                return "修改失败！";
        }
        #endregion
    }
}